package com.doit.demo04;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.Set;

public class test {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//        Reader in = new FileReader("day24\\bean.properties");
        //获取src下配置文件
        InputStream in = test.class.getClassLoader().getResourceAsStream("bean.properties");
        Properties p = new Properties();
        p.load(in);
        in.close();

        String s = p.getProperty("className");
        Class c = Class.forName(s);//创建字节码文件对象
        Object obj = c.newInstance();//创建对象
        Set<String> keys = p.stringPropertyNames();//获取所有的键值对
        for (String key : keys) {
            if (!key.equals("className")){
                //方法名 setName setAge ...
                String methodName = "set"+key.substring(0,1 ).toUpperCase()+key.substring(1);
                //获取方法
                Method method = c.getMethod(methodName, String.class);
                //设置数据
                method.invoke(obj,p.getProperty(key));
            }
        }
        System.out.println(obj);

//        String id = p.getProperty("id");
//        String name = p.getProperty("name");
//        String a = p.getProperty("address");
//
//        Class c = Class.forName(s);
//        Constructor cons = c.getConstructor(String.class, String.class, String.class);
//        Object o = cons.newInstance(id, name, a);
//        System.out.println(o);

    }
}
